package com.lpc.framework.base.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Restful接口的返回值对象
 * 
 * @author jiangjinlong
 */
public class ResultVo {
    /**
     * 失败状态
     */
    public static final int STATUS_FAIL = 0;

    /**
     * 成功状态
     */
    public static final int STATUS_SUCCESS = 1;

    /**
     * https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81#1xx.E6.B6.88.E6.81.AF
     * <p>
     * 4XX表示客户端行为导致的错误 <br>
     * 5XX表示服务端出现错误
     *
     */
    public enum Code {
        // 4XX表示客户端行为导致的错误 403禁止访问 406用户未授权不在白名单中
        Successed(200), Unauthorized(401), FORBIDDEN(403), NotFound(404), MethodNotAllowed(405), UserNotWhiteList(406),
        // 407用户不允许访问URL
        UserNotAllowURL(407), RequiredParamLost(408),

        // 450:业务异常， 451:库存不足 452:优惠计算不匹配 453:失效现金劵 455:收货地址过多 456:不在任何一个配送区域,,457:没有该店铺信息
        // 458:当前时间超过选择的配送时间 459:长时间未完成操作，请重新选购 460提交警告，461：禁止再次提交
        BuzException(450), NotEnough(451), InvalidPromotion(452), InvalidCoupon(453), PromotionOver(454), TooManyReceiver(
                455), OutOfRegion(456), NoSuchShopInfo(457), InvalidEtaTime(458), NoOperateForLongTime(459), SubmitWarning(
                460), ProhibitionOfSubmission(461),
        // 5XX表示服务端出现错误
        ServerError(500);

        private int value;

        private Code(int value) {
            this.value = value;
        }

        public static Code of(Integer value) {
            if (value == null) {
                return null;
            }
            for (Code code : Code.values()) {
                if (value.equals(code.value)) {
                    return code;
                }
            }
            return null;
        }

        public int value() {
            return this.value;
        }

    }

    private int status;// 状态

    /**
     * 错误码，参照HTTP错误码定义
     */
    private Integer code;

    private String message;

    private Object data;// 数据

    public ResultVo() {
        this.fail();
    }

    public ResultVo(int status, Object data) {
        this.status = status;
        this.data = data;
    }

    @JsonIgnore
    public static ResultVo newInstance() {
        return new ResultVo();
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 成功
     */
    @JsonIgnore
    public ResultVo success() {
        this.status = ResultVo.STATUS_SUCCESS;
        this.code = Code.Successed.value;
        return this;
    }

    @JsonIgnore
    public ResultVo data(Object data) {
        this.data = data;
        return this;
    }

    @JsonIgnore
    public ResultVo message(String message) {
        this.message = message;
        return this;
    }

    /**
     * 失败
     */
    @JsonIgnore
    public ResultVo fail() {
        this.status = STATUS_FAIL;
        this.code = Code.BuzException.value;
        return this;
    }

    @JsonIgnore
    public ResultVo code(Code code) {
        this.code = code.value;
        return this;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.status == STATUS_SUCCESS;
    }

    @JsonIgnore
    public ResultVo error(Code code, String message) {
        this.code = code.value;
        this.message = message;
        return this;
    }
}
